<?php

namespace Home\Controller;

use Common\Controller\HomebaseController;

class VideoController extends HomebaseController
{
    public function index()
    {
        $cid = I('get.cid', 2, 'intval');
        $this->commonList($cid);
    }

    public function commonList($cid,$tag)
    {
        $type = I('get.type', 0, 'intval');
        $industry = I('get.industry', 0, 'intval');
        $area = I('get.area', 0, 'intval');
        $time = I('get.time', 0, 'intval');
        if($type > 0){
            $whereArr['type'] = $type;
        }
        if($industry > 0){
            $whereArr['industry'] = $industry;
        }
        if($area > 0){
            $whereArr['area'] = $area;
        }
        if($time > 0){
            $whereArr['time'] = $time;
        }
        $oneCategory = get_one_category($cid);
        $whereArr['status'] = 1;
        if ($cid > 0) {
            $whereArr['cid'] = $cid;
            $currentCategory = get_one_category($cid);
        } else {
            $currentCategory = $oneCategory;
        }
        if($tag){
            $whereArr['tag'] = array('like', '%'. $tag . '%');
            $currentCategory['name'] = $tag .'标签';
        }
        //print_r($whereArr);
        $model = M("Video");
        $pageSize = 20;//20;
        $count = $model
            ->where($whereArr)
            ->count();
        $page = $this->page($count, $pageSize);
        $start = $page->firstRow;
        $list = $model
            ->where($whereArr)
            ->limit($start, $page->listRows)
            ->order("displayorder DESC,created_time DESC")
            ->select();
        //echo $model->getLastSql();
        $page->SetPager('newPage', '<div class="fs0 text-center fen">{first}{prev}{list}{next}{last}</div>');
        if($cid){
            $this->assign("page", str_replace('index.php?g=Home&m=Video&a=index&cid=2&', "video/{$cid}.html?", $page->show('newPage')));
        }else{
            $pageUrl = str_replace('index.php?g=Home&m=Video&a=tag&tag=', "video/tag/", $page->show('newPage'));

            $this->assign("page", str_replace('&p=','?p=',$pageUrl));
        }
        $this->assign("total", $page->getTotalPages());
        $this->assign("p", isset($_GET['p']) ? intval($_GET['p']) : 1);
        $this->assign("list", $list);
        if (!empty($currentCategory['seo_title'])) {
            $seoTitle = $currentCategory['seo_title'] . ' - ';
        } else {
            $seoTitle = $currentCategory['name'] . ' - ';
        }
        $siteOptions = get_site_options();
        $this->assign('site_seo_title', $seoTitle . $siteOptions['site_name']);
        if (!empty($currentCategory['seo_keywords'])) {
            $this->assign('site_seo_keywords', $currentCategory['seo_keywords']);
        }
        if (!empty($currentCategory['seo_description'])) {
            $this->assign('site_seo_description', $currentCategory['seo_description']);
        }
        $this->assign('oneCategory', $oneCategory);
        $this->assign('currentCategory', $currentCategory);
        $this->assign('cid', $cid);
        $this->assign('type', $type);
        $this->assign('industry', $industry);
        $this->assign('area', $area);
        $this->assign('time', $time);
        $this->display('index');
    }

    public function detail()
    {
        $model = M("Video");
        $id = I('get.id', 0, 'intval');
        $whereArr = array(
            'status' => 1,
        );
        if ($id > 0) {
            $whereArr['id'] = $id;
        }
        $post = $model
            ->where($whereArr)
            ->find();
        $model->save(array("id" => $id, "hits" => array("exp", "hits+1")));


        $next = $model
            ->where(array("id" => array('gt', $id), 'status' => 1))
            ->order("created_time asc")
            ->find();

        $prev = $model
            ->where(array("id" => array('lt', $id), 'status' => 1))
            ->order("created_time desc")
            ->find();

        $this->assign("next", $next);
        $this->assign("prev", $prev);

        $smeta = json_decode($post['smeta'], true);
        $this->assign('post', $post);
        $this->assign('cid', $post['cid']);
        $this->assign("smeta", $smeta);
        $siteOptions = get_site_options();
        $this->assign('site_seo_title', $post['title'] . ' - ' . $siteOptions['site_name']);
        if (!empty($post['keywords'])) {
            $this->assign('site_seo_keywords', $post['keywords']);
        }
        if (!empty($post['excerpt'])) {
            $this->assign('site_seo_description', $post['description']);
        }
        $oneCategory = get_one_category(2);
        $currentCategory = get_one_category($post['cid']);
        $this->assign('oneCategory', $oneCategory);
        $this->assign('currentCategory', $currentCategory);
        $next = $model
            ->where(array("id" => array('gt', $id), 'status' => 1))
            ->order("created_time asc")
            ->find();

        $prev = $model
            ->where(array("id" => array('lt', $id), 'status' => 1))
            ->order("created_time desc")
            ->find();
        $whereArr1['cid'] = $post['cid'];
        $relationVideos = get_where_list('video',$whereArr1,5,'hits desc,');
        $this->assign("relationVideos", $relationVideos);
        $this->assign("next", $next);
        $this->assign("prev", $prev);
        $this->display('detail');
    }

    public function tag()
    {
        $tag = I('get.tag');
        $this->commonList(0, $tag);
    }
}